package com.shop.dingdongshoppingapi.service.impl;

import com.shop.dingdongshoppingapi.dao.OrderDao;
import com.shop.dingdongshoppingapi.dto.OrderDto;
import com.shop.dingdongshoppingapi.dto.OrderAddDto;
import com.shop.dingdongshoppingapi.entity.Order;
import com.shop.dingdongshoppingapi.service.intf.OrderService;
import com.shop.dingdongshoppingapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

/***
 *  .--,       .--,
 * ( (  .---./  ) )
 *  '.__/o   o\__.'
 *     {=  ^  =}
 *      >  -  <
 *     /
 *    //       \
 *   //|   .   |\
 *   "'       /'"_.-~^`'-.
 *        _  /--'         `
 *    ___)( )(___
 *   (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之。
 */


@Service
public class OrderServiceImpl implements OrderService {
	@Resource
	private OrderDao dao;

	@Override
	public R selectInfo(Integer id) {
		if (id > 0) {
			//2.查询数据库 支持手机号或用户名 登录
			List<OrderDto> orders = dao.selectMoods(id);
			//3.验证是否存在用户
			if (orders != null) {
				return R.ok(orders);
			}
		}
		return R.fail();
	}

	@Override
	public R add(int uid, OrderAddDto dto) {
		if (uid > 0 && dto != null) {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = sdf.format(date);

			int count = 0;

			for (int i = 0; i < dto.getGoods().size(); i++) {
				Order order = new Order(format, dto.getGname(), dto.getGimage(), uid, dto.getAddid(), dto.getGoods().get(i).getGid(), dto.getGoods().get(i).getGnum(), dto.getInfo(), dto.getGprice(), date);
				dao.insert(order);
				count++;
			}
			if (count > 0) {
				return R.ok(format);
			}
		}
		return R.fail();
	}
}
